package com.yyw.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yyw.bean.Setmeal;
import com.yyw.common.PageParam;
import com.yyw.common.R;
import com.yyw.dto.SetmealDto;
import com.yyw.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/setmeal")
@RestController
public class SetmealController {

    @Autowired
    private SetmealService setmealService;


    /**
     * 添加套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R add(@RequestBody SetmealDto setmealDto){
        int row = setmealService.add(setmealDto);

        if (row > 0){
            return R.success("套餐添加成功！");
        }
        return R.error("套餐添加失败！");
    }


    /**
     * 套餐分页查询
     * @param pageParam
     * @return
     */
    @GetMapping("/page")
    public R findPage(PageParam pageParam){
        IPage<SetmealDto> page = setmealService.findPage(pageParam);

        return R.success(page);
    }


    /**
     * 删除套餐，不管是删除单个还是批量删除都访问该方法
     * @param ids
     * @return
     */
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids){
        //
        int row = setmealService.delete(ids);

        //
        if(row > 0 ){
            return R.success("套餐删除成功！");
        }
        return R.error("套餐删除失败！");
    }


    /**
     * 根据类型查询套餐，只查询处于起售状态的套餐
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("/list")
    public R findByCategoryId(Long categoryId , Integer status){
        List<Setmeal> setmealList = setmealService.findByCategoryId(categoryId, status);

        return R.success(setmealList);

    }

}
